package com.vivek.guess.what.calculation;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.vivz.guess.what.type.Position;
import com.vivz.guess.what.vo.Clue;
import com.vivz.guess.what.vo.Guess;

public class AllXCalculation extends AbstractCalculation {

	@Override
	public void calculate(List<Guess> guesses, Map<Character, Set<Position>> probabilityMap) {

		if (isInputValid(guesses, probabilityMap)) {

			for (Guess guessX : guesses) {
				Clue clueX = guessX.getClue();

				if (!clueX.hasDots() && clueX.hasCrosses()) {

					String word = guessX.getWord();
					for (Character ch : word.toCharArray()) {
						if (probabilityMap.containsKey(ch)) {
							Set<Position> positions = probabilityMap.get(ch);
							Position position = Position.getInstance(word.indexOf(ch) + 1);
							positions.retainAll(Collections.singleton(position));
						}
					}
				}
			}

			removeEmptyPositionSets(probabilityMap);
		}
	}

}
